package com.lhd._05mvcboot03_1.controller;

import com.lhd._05mvcboot03_1.common.response.JsonResult;
import com.lhd._05mvcboot03_1.common.response.StatusCode;
import com.lhd._05mvcboot03_1.mapper.UserArticleMapper;
import com.lhd._05mvcboot03_1.pojo.dto.UserArticleDTO;
import com.lhd._05mvcboot03_1.pojo.dto.UserLoginDTO;
import com.lhd._05mvcboot03_1.pojo.dto.UserRegDTO;
import com.lhd._05mvcboot03_1.pojo.entity.Article;
import com.lhd._05mvcboot03_1.pojo.entity.User;
import com.lhd._05mvcboot03_1.pojo.vo.ArticleVO;
import com.lhd._05mvcboot03_1.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author lhd
 * @date 2024/4/28
 * @apiNote
 */
@Api(tags ="用户和文章模块")
@Validated
@RestController
@Slf4j
@RequestMapping("v1/users/")
public class UserArticleController {
    @Autowired
    private UserArticleMapper userArticleMapper;

    @PostMapping(value ="reg")
    @ApiOperation(value ="注册功能")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO){
        log.debug("注册功能");

        //检查用户名是否被占用
        UserVO uesVO = userArticleMapper.selectByUsername(userRegDTO.getUsername());
        if (uesVO != null){
            return new JsonResult(StatusCode.USERNAME_EXISTS_ERROR);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        System.out.println(
                user
        );
        userArticleMapper.insert(user);

        return JsonResult.ok();
    }
    @PostMapping(value ="login")
    @ApiOperation(value ="登录功能")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        log.debug("登录功能");
        UserVO userVO = userArticleMapper.selectByUsernameAndPassword(userLoginDTO.getUsername(),userLoginDTO.getPassword());
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(StatusCode.OPERATION_SUCCESS);
    }

    @PostMapping(value ="article")
    @ApiOperation(value ="发表文章功能")
    public JsonResult article(@RequestBody @Validated UserArticleDTO userArticleDTO,@ApiIgnore HttpSession session){
        log.debug("发表文章功能");

        UserVO userVO = (UserVO)session.getAttribute("user");
        //检验会否登录
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        //登录执行插入文章
        Article article = new Article();
        BeanUtils.copyProperties(userArticleDTO,article);
        article.setUserId(userVO.getId());
        // 插入文章
        userArticleMapper.insertArticle(article);

        return  JsonResult.ok(StatusCode.OPERATION_SUCCESS);

    }

    @GetMapping(value ="selectByArticleId")
    @ApiOperation("查询指定文章功能")
    public JsonResult selectByArticleId(    @Range(min = 1, max = 100 ,message = "文章id必须在1-100之间")
                                            @RequestParam("id") Long id, @ApiIgnore HttpSession session){
        log.debug("查询指定文章功能 查询文章 id :" + id);
        UserVO userVO = (UserVO)session.getAttribute("user");
        //检验会否登录
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        ArticleVO articleVO = userArticleMapper.selectByArticleId(id);

        return JsonResult.ok(articleVO);
    }

}
